// pages/h5/pay.js   定制界面 无小程序下单支付
Page({
  /**
   * 页面的初始数据
   */
  data: {
    payDate: {
      openID: '',
      merchantID: '',
      money: '',
      body: '',
      note: '',
      external_order_no: '',  //外部订单号
      notify_url: '',   //异步回调地址
    }
  },

  pay(paydata) {
    wx.request({
      url: 'https://openpay.duolaibei.com/mobile/wechatweb',
      data: paydata,
      method: "POST",
      success(res) {
        if (res.data.code == "200") {
          let successData = res.data.content;
          wx.requestPayment({
            timeStamp: successData.payData.timeStamp,
            nonceStr: successData.payData.nonceStr,
            package: successData.payData.package,
            signType: successData.payData.signType,
            paySign: successData.payData.paySign,
            success(res) {
              wx.reLaunch({
                // url: '/pages/webview/webview?status=1&order_no=' + successData.orderNo
                url: '/pages/custom/nfty?status=1'
              })
            },
            fail(res) {
              if (res.errMsg == 'requestPayment:fail cancel') { //支付取消
                wx.reLaunch({
                   //url: '/pages/webview/webview?status=2&order_no=' + successData.orderNo
                  url: '/pages/custom/nfty?status=2'
                })
              } else {
                wx.reLaunch({
                  // url: '/pages/webview/webview?status=3&order_no=' + successData.orderNo
                  url: '/pages/custom/nfty?status=3'
                })
              }
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            success(res) {
              if (res.confirm) { //确定
                wx.reLaunch({
                  // url: '/pages/webview/webview?status=4&order_no=' + successData.orderNo
                  url: '/pages/custom/nfty?status=4'
                })
              } else if (res.cancel) { //取消
                wx.reLaunch({
                  // url: '/pages/webview/webview?status=2&order_no=' + successData.orderNo
                  url: '/pages/custom/nfty?status=2'
                })
              }
            }
          });
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.login({
      success: (res) => {
        if (res.code) {
          wx.request({
            url: 'https://openpay.duolaibei.com/mobile/getOpenID',
            data: {
              type: 1,
              code: res.code,
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded' //修改此处即可
            },
            method: 'POST',
            success(res) {
              if (res.statusCode == "200") { //获取openID成功 下单支付
                that.setData({
                  ['payDate.merchantID']: options.merchantID,
                  ['payDate.money']: options.money,
                  ['payDate.body']: options.body ? options.body : options.merchantName,
                  ['payDate.note']: options.note,
                  ['payDate.openID']: res.data.content.openid,
                  ['payDate.external_order_no']: options.external_order_no,
                  ['payDate.notify_url']: options.notify_url,
                });
                that.pay(that.data.payDate);
              }
            }
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})